using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace BitMango
{
	public static class StringExt
	{
		public static string Base64Encode(this string src, Encoding enc)
		{
			byte[] bytes = enc.GetBytes(src);
			return Convert.ToBase64String(bytes);
		}

		public static string Base64Decode(this string src, Encoding enc)
		{
			byte[] bytes = Convert.FromBase64String(src);
			return enc.GetString(bytes);
		}

		public static bool IsBase64String(string s)
		{
			s = s.Trim();
			return s.Length % 4 == 0 && Regex.IsMatch(s, "^[a-zA-Z0-9\\+/]*={0,3}$", RegexOptions.None);
		}

		public static Dictionary<string, string> parseQueryString(this string src)
		{
			string[] array = src.Split(new char[]
			{
				'&'
			});
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			for (int i = 0; i < array.Length; i += 2)
			{
				dictionary[array[i]] = array[i + 1];
			}
			return dictionary;
		}

		public static bool Like(this string str, string wildcard)
		{
			return new Regex("^" + Regex.Escape(wildcard).Replace("\\*", ".*").Replace("\\?", ".") + "$", RegexOptions.IgnoreCase | RegexOptions.Singleline).IsMatch(str);
		}
	}
}
